package Tree;

import java.util.Stack;

public class SumOfLeftLeaves {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        SumOfLeftLeaves solution = new SumOfLeftLeaves();
        System.out.println(solution.sumOfLeftLeaves(root));

    }

    public int sumOfLeftLeaves(TreeNode root){
        if(root == null) return 0;
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        int result = 0;
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            if(node.left != null && node.left.left == null && node.left.right == null){
                result += node.left.val;
            }
            if (node.left != null) stack.add(node.left);
            if (node.right != null) stack.add(node.right);
        }
        return result;
    }
}
